package com.ftm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ftm.dao.AdminDao;
import com.ftm.dao.UserDao;
import com.ftm.dao.impl.AdminDaoImpl;
import com.ftm.dao.impl.UserDaoImpl;
import com.ftm.entity.Admin;
import com.ftm.service.AdminService;
import com.ftm.service.impl.AdminServiceImpl;
import com.ftm.tools.MD5Util;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/BackstageLoginServlet")
public class BackstageLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BackstageLoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;chaeset=utf-8");
		request.setCharacterEncoding("utf-8");
		String adminuser = request.getParameter("adminuser");
		String adminpwd = request.getParameter("adminpwd");
		System.out.println("adminpwd====================="+adminpwd);
		// 加密处理
		MD5Util md5 = new MD5Util();
		adminpwd = md5.getEncodeByMd5(adminpwd);
		// 设置op变量表示用户登入操作
		String op = "";
		// 判断是否是登入操作
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);
		// 如果是登录操作，则进行帐号密码判断
		if ("login".equals(op)) {
			AdminDao ad = new AdminDaoImpl();
			Admin admin = ad.queryAdmin(adminuser, adminpwd);
			if (admin != null) {
				request.getSession().setAttribute("admin", admin);
				// 用户选择了记住密码
				if (request.getParameter("cksremember") != null) {
					// 创建cookie，并设置其过期时间，存储到本地
					Cookie cookie1 = new Cookie("adminuser", adminuser);
					cookie1.setMaxAge(60 * 60);// 1h

					Cookie cookie2 = new Cookie("adminpwd", request.getParameter("adminpwd"));
					cookie2.setMaxAge(60 * 60);
					// 将cookie的信息存储在本地: addCookie
					response.addCookie(cookie1);
					response.addCookie(cookie2);
				}
				request.getRequestDispatcher("BackstageServlet?op=queryalladmin&adminuser=" + adminuser)
						.forward(request, response);
			} else {
				// 否则跳转到login.jsp登录窗口
				response.sendRedirect("backstagelogin.jsp");
			}
		} else {
			// 否则跳转到login.jsp登录窗口
			response.sendRedirect("backstagelogin.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
